<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Drawing - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.pdf.drawing.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.pdf.drawing.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.pdf.pages.html">Working with Pages</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.pdf.html">Zend_Pdf</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.pdf.interactive-features.html">Interactive Features</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.pdf.drawing" class="section"><div class="info"><h1 class="title">Drawing</h1></div>
    

    <div class="section" id="zend.pdf.drawing.geometry"><div class="info"><h1 class="title">Geometry</h1></div>
        

        <p class="para">
            <acronym class="acronym">PDF</acronym> uses the same geometry as PostScript. It starts from bottom-left
            corner of page and by default is measured in points (1/72 of an inch).
        </p>

        <p class="para">
            Page size can be retrieved from a page object:
        </p>

        <pre class="programlisting brush: php">
$width  = $pdfPage-&gt;getWidth();
$height = $pdfPage-&gt;getHeight();
</pre>

    </div>

    <div class="section" id="zend.pdf.drawing.color"><div class="info"><h1 class="title">Colors</h1></div>
        

        <p class="para">
            <acronym class="acronym">PDF</acronym> has a powerful capabilities for colors representation.
            <span class="classname">Zend_Pdf</span> module supports Gray Scale, RGB and CMYK color spaces.
            Any of them can be used in any place, where <span class="classname">Zend_Pdf_Color</span> object
            is required. <span class="classname">Zend_Pdf_Color_GrayScale</span>,
            <span class="classname">Zend_Pdf_Color_Rgb</span> and <span class="classname">Zend_Pdf_Color_Cmyk</span>
            classes provide this functionality:
        </p>

        <pre class="programlisting brush: php">
// $grayLevel (float number). 0.0 (black) - 1.0 (white)
$color1 = new Zend_Pdf_Color_GrayScale($grayLevel);

// $r, $g, $b (float numbers). 0.0 (min intensity) - 1.0 (max intensity)
$color2 = new Zend_Pdf_Color_Rgb($r, $g, $b);

// $c, $m, $y, $k (float numbers). 0.0 (min intensity) - 1.0 (max intensity)
$color3 = new Zend_Pdf_Color_Cmyk($c, $m, $y, $k);
</pre>


        <p class="para">
            <acronym class="acronym">HTML</acronym> style colors are also provided with
            <span class="classname">Zend_Pdf_Color_Html</span> class:
        </p>

        <pre class="programlisting brush: php">
$color1 = new Zend_Pdf_Color_Html(&#039;#3366FF&#039;);
$color2 = new Zend_Pdf_Color_Html(&#039;silver&#039;);
$color3 = new Zend_Pdf_Color_Html(&#039;forestgreen&#039;);
</pre>

    </div>

    <div class="section" id="zend.pdf.drawing.shape-drawing"><div class="info"><h1 class="title">Shape Drawing</h1></div>
        

        <p class="para">
            All drawing operations can be done in a context of <acronym class="acronym">PDF</acronym> page.
        </p>

        <p class="para">
            <span class="classname">Zend_Pdf_Page</span> class provides a set of drawing primitives:
        </p>

        <pre class="programlisting brush: php">
/**
 * Draw a line from x1,y1 to x2,y2.
 *
 * @param float $x1
 * @param float $y1
 * @param float $x2
 * @param float $y2
 * @return Zend_Pdf_Page
 */
public function drawLine($x1, $y1, $x2, $y2);
</pre>


        <pre class="programlisting brush: php">
/**
 * Draw a rectangle.
 *
 * Fill types:
 * Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE - fill rectangle
 *                                             and stroke (default)
 * Zend_Pdf_Page::SHAPE_DRAW_STROKE          - stroke rectangle
 * Zend_Pdf_Page::SHAPE_DRAW_FILL            - fill rectangle
 *
 * @param float $x1
 * @param float $y1
 * @param float $x2
 * @param float $y2
 * @param integer $fillType
 * @return Zend_Pdf_Page
 */
public function drawRectangle($x1, $y1, $x2, $y2,
                    $fillType = Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE);
</pre>


        <pre class="programlisting brush: php">
/**
 * Draw a rounded rectangle.
 *
 * Fill types:
 * Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE - fill rectangle
 *                                             and stroke (default)
 * Zend_Pdf_Page::SHAPE_DRAW_STROKE      - stroke rectangle
 * Zend_Pdf_Page::SHAPE_DRAW_FILL        - fill rectangle
 *
 * radius is an integer representing radius of the four corners, or an array
 * of four integers representing the radius starting at top left, going
 * clockwise
 *
 * @param float $x1
 * @param float $y1
 * @param float $x2
 * @param float $y2
 * @param integer|array $radius
 * @param integer $fillType
 * @return Zend_Pdf_Page
 */
public function drawRoundedRectangle($x1, $y1, $x2, $y2, $radius,
                       $fillType = Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE);
</pre>


        <pre class="programlisting brush: php">
/**
 * Draw a polygon.
 *
 * If $fillType is Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE or
 * Zend_Pdf_Page::SHAPE_DRAW_FILL, then polygon is automatically closed.
 * See detailed description of these methods in a PDF documentation
 * (section 4.4.2 Path painting Operators, Filling)
 *
 * @param array $x  - array of float (the X co-ordinates of the vertices)
 * @param array $y  - array of float (the Y co-ordinates of the vertices)
 * @param integer $fillType
 * @param integer $fillMethod
 * @return Zend_Pdf_Page
 */
public function drawPolygon($x, $y,
                            $fillType =
                                Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE,
                            $fillMethod =
                                Zend_Pdf_Page::FILL_METHOD_NON_ZERO_WINDING);
</pre>


        <pre class="programlisting brush: php">
/**
 * Draw a circle centered on x, y with a radius of radius.
 *
 * Angles are specified in radians
 *
 * Method signatures:
 * drawCircle($x, $y, $radius);
 * drawCircle($x, $y, $radius, $fillType);
 * drawCircle($x, $y, $radius, $startAngle, $endAngle);
 * drawCircle($x, $y, $radius, $startAngle, $endAngle, $fillType);
 *
 *
 * It&#039;s not a really circle, because PDF supports only cubic Bezier
 * curves. But very good approximation.
 * It differs from a real circle on a maximum 0.00026 radiuses (at PI/8,
 * 3*PI/8, 5*PI/8, 7*PI/8, 9*PI/8, 11*PI/8, 13*PI/8 and 15*PI/8 angles).
 * At 0, PI/4, PI/2, 3*PI/4, PI, 5*PI/4, 3*PI/2 and 7*PI/4 it&#039;s exactly
 * a tangent to a circle.
 *
 * @param float $x
 * @param float $y
 * @param float $radius
 * @param mixed $param4
 * @param mixed $param5
 * @param mixed $param6
 * @return Zend_Pdf_Page
 */
public function  drawCircle($x,
                            $y,
                            $radius,
                            $param4 = null,
                            $param5 = null,
                            $param6 = null);
</pre>


        <pre class="programlisting brush: php">
/**
 * Draw an ellipse inside the specified rectangle.
 *
 * Method signatures:
 * drawEllipse($x1, $y1, $x2, $y2);
 * drawEllipse($x1, $y1, $x2, $y2, $fillType);
 * drawEllipse($x1, $y1, $x2, $y2, $startAngle, $endAngle);
 * drawEllipse($x1, $y1, $x2, $y2, $startAngle, $endAngle, $fillType);
 *
 * Angles are specified in radians
 *
 * @param float $x1
 * @param float $y1
 * @param float $x2
 * @param float $y2
 * @param mixed $param5
 * @param mixed $param6
 * @param mixed $param7
 * @return Zend_Pdf_Page
 */
public function drawEllipse($x1,
                            $y1,
                            $x2,
                            $y2,
                            $param5 = null,
                            $param6 = null,
                            $param7 = null);
</pre>

    </div>

    <div class="section" id="zend.pdf.drawing.text-drawing"><div class="info"><h1 class="title">Text Drawing</h1></div>
        

        <p class="para">
            Text drawing operations also exist in the context of a <acronym class="acronym">PDF</acronym> page. You
            can draw a single line of text at any position on the page by supplying the x and y
            coordinates of the baseline. Current font, font size and page fill color are used for text
            drawing operations (see detailed description below).
        </p>

        <pre class="programlisting brush: php">
/**
 * Draw a line of text at the specified position.
 *
 * @param string $text
 * @param float $x
 * @param float $y
 * @param string $charEncoding (optional) Character encoding of source
 *               text.Defaults to current locale.
 * @throws Zend_Pdf_Exception
 * @return Zend_Pdf_Page
 */
public function drawText($text, $x, $y, $charEncoding = &#039;&#039;);
</pre>


        <div class="example" id="zend.pdf.drawing.text-drawing.example-1"><div class="info"><p><b>Example #1 Draw a string on the page</b></p></div>
            

            <pre class="programlisting brush: php">
...
$pdfPage-&gt;drawText(&#039;Hello world!&#039;, 72, 720);
...
</pre>

        </div>

        <div class="example" id="zend.pdf.drawing.text-drawing.example-2"><div class="info"><p><b>Example #2 Set font color</b></p></div>
            

            <pre class="programlisting brush: php">
...
$pdfPage-&gt;setFillColor(Zend_Pdf_Color_Html::color(&#039;#990000&#039;))
        -&gt;drawText(&#039;Hello world (in red)!&#039;, 72, 720);
....
</pre>

        </div>


        <p class="para">
            By default, text strings are interpreted using the character encoding method of the
            current locale. if you have a string that uses a different encoding method (such as a
            UTF-8 string read from a file on disk, or a MacRoman string obtained from a legacy
            database), you can indicate the character encoding at draw time and
            <span class="classname">Zend_Pdf</span> will handle the conversion for you. You can supply
            source strings in any encoding method supported by <acronym class="acronym">PHP</acronym>&#039;s
            <a href="http://www.php.net/manual/function.iconv.php" class="link external">&raquo; iconv()</a>
            function:
        </p>

        <div class="example" id="zend.pdf.drawing.text-drawing.example-3"><div class="info"><p><b>Example #3 Draw a UTF-8-encoded string on the page</b></p></div>
            

            <pre class="programlisting brush: php">
...
// Read a UTF-8-encoded string from disk
$unicodeString = fread($fp, 1024);

// Draw the string on the page
$pdfPage-&gt;drawText($unicodeString, 72, 720, &#039;UTF-8&#039;);
...
</pre>

        </div>
    </div>

    <div class="section" id="zend.pdf.drawing.using-fonts"><div class="info"><h1 class="title">Using fonts</h1></div>
        

        <p class="para">
             <span class="methodname">Zend_Pdf_Page::drawText()</span> uses the page&#039;s current font and font
            size, which is set with the  <span class="methodname">Zend_Pdf_Page::setFont()</span> method:
        </p>

        <pre class="programlisting brush: php">
/**
 * Set current font.
 *
 * @param Zend_Pdf_Resource_Font $font
 * @param float $fontSize
 * @return Zend_Pdf_Page
 */
public function setFont(Zend_Pdf_Resource_Font $font, $fontSize);
</pre>


        <p class="para">
            <acronym class="acronym">PDF</acronym> documents support PostScript Type 1 and TrueType fonts, as well
            as two specialized <acronym class="acronym">PDF</acronym> types, Type 3 and composite fonts. There are
            also 14 standard Type 1 fonts built-in to every <acronym class="acronym">PDF</acronym> viewer: Courier
            (4 styles), Helvetica (4 styles), Times (4 styles), Symbol, and Zapf Dingbats.
        </p>

        <p class="para">
            <span class="classname">Zend_Pdf</span> currently supports the standard 14
            <acronym class="acronym">PDF</acronym> fonts as well as your own custom TrueType fonts. Font objects are
            obtained via one of two factory methods:
             <span class="methodname">Zend_Pdf_Font::fontWithName($fontName)</span> for the standard 14
            <acronym class="acronym">PDF</acronym> fonts or
             <span class="methodname">Zend_Pdf_Font::fontWithPath($filePath)</span> for custom fonts.
        </p>

        <div class="example" id="zend.pdf.drawing.using-fonts.example-1"><div class="info"><p><b>Example #4 Create a standard font</b></p></div>
            

            <pre class="programlisting brush: php">
...
// Create new font
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);

// Apply font
$pdfPage-&gt;setFont($font, 36);
...
</pre>

        </div>

        <p class="para">
            Constants for the standard 14 <acronym class="acronym">PDF</acronym> font names are defined in the
            <span class="classname">Zend_Pdf_Font</span> class:
            <ul class="itemizedlist">
                <li class="listitem"><p class="para">Zend_Pdf_Font::FONT_COURIER</p></li>
                <li class="listitem"><p class="para">Zend_Pdf_Font::FONT_COURIER_BOLD</p></li>
                <li class="listitem"><p class="para">Zend_Pdf_Font::FONT_COURIER_ITALIC</p></li>
                <li class="listitem"><p class="para">Zend_Pdf_Font::FONT_COURIER_BOLD_ITALIC</p></li>
                <li class="listitem"><p class="para">Zend_Pdf_Font::FONT_TIMES</p></li>
                <li class="listitem"><p class="para">Zend_Pdf_Font::FONT_TIMES_BOLD</p></li>
                <li class="listitem"><p class="para">Zend_Pdf_Font::FONT_TIMES_ITALIC</p></li>
                <li class="listitem"><p class="para">Zend_Pdf_Font::FONT_TIMES_BOLD_ITALIC</p></li>
                <li class="listitem"><p class="para">Zend_Pdf_Font::FONT_HELVETICA</p></li>
                <li class="listitem"><p class="para">Zend_Pdf_Font::FONT_HELVETICA_BOLD</p></li>
                <li class="listitem"><p class="para">Zend_Pdf_Font::FONT_HELVETICA_ITALIC</p></li>
                <li class="listitem"><p class="para">Zend_Pdf_Font::FONT_HELVETICA_BOLD_ITALIC</p></li>
                <li class="listitem"><p class="para">Zend_Pdf_Font::FONT_SYMBOL</p></li>
                <li class="listitem"><p class="para">Zend_Pdf_Font::FONT_ZAPFDINGBATS</p></li>
            </ul>
        </p>

        <p class="para">
            You can also use any individual TrueType font (which usually has a &#039;.ttf&#039; extension) or
            an OpenType font (&#039;.otf&#039; extension) if it contains TrueType outlines. Currently
            unsupported, but planned for a future release are Mac OS X .dfont files and Microsoft
            TrueType Collection (&#039;.ttc&#039; extension) files.
        </p>

        <p class="para">
            To use a TrueType font, you must provide the full file path to the font program. If the
            font cannot be read for some reason, or if it is not a TrueType font, the factory method
            will throw an exception:
        </p>

        <div class="example" id="zend.pdf.drawing.using-fonts.example-2"><div class="info"><p><b>Example #5 Create a TrueType font</b></p></div>
            

            <pre class="programlisting brush: php">
...
// Create new font
$goodDogCoolFont = Zend_Pdf_Font::fontWithPath(&#039;/path/to/GOODDC__.TTF&#039;);

// Apply font
$pdfPage-&gt;setFont($goodDogCoolFont, 36);
...
</pre>

        </div>

        <p class="para">
            By default, custom fonts will be embedded in the resulting <acronym class="acronym">PDF</acronym>
            document. This allows recipients to view the page as intended, even if they don&#039;t have
            the proper fonts installed on their system. If you are concerned about file size, you
            can request that the font program not be embedded by passing a &#039;do not embed&#039; option to
            the factory method:
        </p>

        <div class="example" id="zend.pdf.drawing.using-fonts.example-3"><div class="info"><p><b>Example #6 Create a TrueType font, but do not embed it in the PDF document</b></p></div>
            

            <pre class="programlisting brush: php">
...
// Create new font
$goodDogCoolFont = Zend_Pdf_Font::fontWithPath(&#039;/path/to/GOODDC__.TTF&#039;,
                                               Zend_Pdf_Font::EMBED_DONT_EMBED);

// Apply font
$pdfPage-&gt;setFont($goodDogCoolFont, 36);
...
</pre>

        </div>

        <p class="para">
            If the font program is not embedded but the recipient of the <acronym class="acronym">PDF</acronym> file
            has the font installed on their system, they will see the document as intended. If they
            do not have the correct font installed, the <acronym class="acronym">PDF</acronym> viewer application
            will do its best to synthesize a replacement.
        </p>

        <p class="para">
            Some fonts have very specific licensing rules which prevent them from being embedded in
            <acronym class="acronym">PDF</acronym> documents. So you are not caught off-guard by this, if you try to
            use a font that cannot be embedded, the factory method will throw an exception.
        </p>

        <p class="para">
            You can still use these fonts, but you must either pass the do not embed flag as
            described above, or you can simply suppress the exception:
        </p>

        <div class="example" id="zend.pdf.drawing.using-fonts.example-4"><div class="info"><p><b>Example #7 Do not throw an exception for fonts that cannot be embedded</b></p></div>
            

            <pre class="programlisting brush: php">
...
$font = Zend_Pdf_Font::fontWithPath(
           &#039;/path/to/unEmbeddableFont.ttf&#039;,
           Zend_Pdf_Font::EMBED_SUPPRESS_EMBED_EXCEPTION
        );
...
</pre>

        </div>

        <p class="para">
            This suppression technique is preferred if you allow an end-user to choose their own
            fonts. Fonts which can be embedded in the <acronym class="acronym">PDF</acronym> document will be; those
            that cannot, won&#039;t.
        </p>

        <p class="para">
            Font programs can be rather large, some reaching into the tens of megabytes. By default,
            all embedded fonts are compressed using the Flate compression scheme, resulting in a
            space savings of 50% on average. If, for some reason, you do not want to compress the
            font program, you can disable it with an option:
        </p>

        <div class="example" id="zend.pdf.drawing.using-fonts.example-5"><div class="info"><p><b>Example #8 Do not compress an embedded font</b></p></div>
            

            <pre class="programlisting brush: php">
...
$font = Zend_Pdf_Font::fontWithPath(&#039;/path/to/someReallyBigFont.ttf&#039;,
                                    Zend_Pdf_Font::EMBED_DONT_COMPRESS);
...
</pre>

        </div>

        <p class="para">
            Finally, when necessary, you can combine the embedding options by using the bitwise OR
            operator:
        </p>

        <div class="example" id="zend.pdf.drawing.using-fonts.example-6"><div class="info"><p><b>Example #9 Combining font embedding options</b></p></div>
            

            <pre class="programlisting brush: php">
...
$font = Zend_Pdf_Font::fontWithPath(
            $someUserSelectedFontPath,
            (Zend_Pdf_Font::EMBED_SUPPRESS_EMBED_EXCEPTION |
            Zend_Pdf_Font::EMBED_DONT_COMPRESS));
...
</pre>

        </div>
    </div>

    <div class="section" id="zend.pdf.drawing.standard-fonts-limitations"><div class="info"><h1 class="title">Standard PDF fonts limitations</h1></div>
        

        <p class="para">
            Standard <acronym class="acronym">PDF</acronym> fonts use several single byte encodings internally
            (see <a href="http://www.adobe.com/devnet/acrobat/pdfs/pdf_reference_1-7.pdf" class="link external">&raquo; PDF
                Reference, Sixth Edition, version 1.7</a> Appendix D for details). They are
            generally equal to Latin1 character set (except Symbol and ZapfDingbats fonts).
        </p>

        <p class="para">
            <span class="classname">Zend_Pdf</span> uses CP1252 (WinLatin1) for drawing text with standard
            fonts.
        </p>

        <p class="para">
            Text still can be provided in any other encoding, which must be specified if it differs
            from a current locale. Only WinLatin1 characters will be actually drawn.
        </p>

        <div class="example" id="zend.pdf.drawing.using-fonts.example-7"><div class="info"><p><b>Example #10 Combining font embedding options</b></p></div>
            

            <pre class="programlisting brush: php">
...
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_COURIER);
$pdfPage-&gt;setFont($font, 36)
        -&gt;drawText(&#039;Euro sign - €&#039;, 72, 720, &#039;UTF-8&#039;)
        -&gt;drawText(&#039;Text with umlauts - à è ì&#039;, 72, 650, &#039;UTF-8&#039;);
...
</pre>

        </div>
    </div>

    <div class="section" id="zend.pdf.drawing.extracting-fonts"><div class="info"><h1 class="title">Extracting fonts</h1></div>
        

        <p class="para">
            <span class="classname">Zend_Pdf</span> module provides a possibility to extract fonts from
            loaded documents.
        </p>

        <p class="para">
            It may be useful for incremental document updates. Without this functionality you have
            to attach and possibly embed font into a document each time you want to update it.
        </p>

        <p class="para">
            <span class="classname">Zend_Pdf</span> and <span class="classname">Zend_Pdf_Page</span> objects provide
            special methods to extract all fonts mentioned within a document or a page:
        </p>

        <div class="example" id="zend.pdf.drawing.extracting-fonts.example-1"><div class="info"><p><b>Example #11 Extracting fonts from a loaded document</b></p></div>
            

            <pre class="programlisting brush: php">
...
$pdf = Zend_Pdf::load($documentPath);
...
// Get all document fonts
$fontList = $pdf-&gt;extractFonts();
$pdf-&gt;pages[] = ($page = $pdf-&gt;newPage(Zend_Pdf_Page::SIZE_A4));
$yPosition = 700;
foreach ($fontList as $font) {
    $page-&gt;setFont($font, 15);
    $fontName = $font-&gt;getFontName(Zend_Pdf_Font::NAME_POSTSCRIPT,
                                   &#039;en&#039;,
                                   &#039;UTF-8&#039;);
    $page-&gt;drawText($fontName . &#039;: The quick brown fox jumps over the lazy dog&#039;,
                    100,
                    $yPosition,
                    &#039;UTF-8&#039;);
    $yPosition -= 30;
}
...
// Get fonts referenced within the first document page
$firstPage = reset($pdf-&gt;pages);
$firstPageFonts = $firstPage-&gt;extractFonts();
...
</pre>

        </div>

        <div class="example" id="zend.pdf.drawing.extracting-fonts.example-2"><div class="info"><p><b>Example #12 Extracting font from a loaded document by specifying font name</b></p></div>
            

            <pre class="programlisting brush: php">
...
$pdf = new Zend_Pdf();
...
$pdf-&gt;pages[] = ($page = $pdf-&gt;newPage(Zend_Pdf_Page::SIZE_A4));

$font = Zend_Pdf_Font::fontWithPath($fontPath);
$page-&gt;setFont($font, $fontSize);
$page-&gt;drawText($text, $x, $y);
...
// This font name should be stored somewhere...
$fontName = $font-&gt;getFontName(Zend_Pdf_Font::NAME_POSTSCRIPT,
                               &#039;en&#039;,
                               &#039;UTF-8&#039;);
...
$pdf-&gt;save($docPath);
...
</pre>


            <pre class="programlisting brush: php">
...
$pdf = Zend_Pdf::load($docPath);
...
$pdf-&gt;pages[] = ($page = $pdf-&gt;newPage(Zend_Pdf_Page::SIZE_A4));

/* $srcPage-&gt;extractFont($fontName) can also be used here */
$font = $pdf-&gt;extractFont($fontName);

$page-&gt;setFont($font, $fontSize);
$page-&gt;drawText($text, $x, $y);
...
$pdf-&gt;save($docPath, true /* incremental update mode */);
...
</pre>

        </div>

        <p class="para">
            Extracted fonts can be used in the place of any other font with the following
            limitations:

            <ul class="itemizedlist">
                <li class="listitem">
                    <p class="para">
                        Extracted font can be used only in the context of the document from which it
                        was extracted.
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                        Possibly embedded font program is actually not extracted. So extracted font
                        can&#039;t provide correct font metrics and original font has to be used for text
                        width calculations:
                    </p>

                    <pre class="programlisting brush: php">
...
$font = $pdf-&gt;extractFont($fontName);
$originalFont = Zend_Pdf_Font::fontWithPath($fontPath);

$page-&gt;setFont($font /* use extracted font for drawing */, $fontSize);
$xPosition = $x;
for ($charIndex = 0; $charIndex &lt; strlen($text); $charIndex++) {
    $page-&gt;drawText($text[$charIndex], xPosition, $y);

    // Use original font for text width calculation
    $width = $originalFont-&gt;widthForGlyph(
                 $originalFont-&gt;glyphNumberForCharacter($text[$charIndex])
             );
    $xPosition += $width/$originalFont-&gt;getUnitsPerEm()*$fontSize;
}
...
</pre>

                </li>
            </ul>
        </p>
    </div>

    <div class="section" id="zend.pdf.drawing.image-drawing"><div class="info"><h1 class="title">Image Drawing</h1></div>
        

        <p class="para">
            <span class="classname">Zend_Pdf_Page</span> class provides drawImage() method to draw image:
        </p>

        <pre class="programlisting brush: php">
/**
 * Draw an image at the specified position on the page.
 *
 * @param Zend_Pdf_Resource_Image $image
 * @param float $x1
 * @param float $y1
 * @param float $x2
 * @param float $y2
 * @return Zend_Pdf_Page
 */
public function drawImage(Zend_Pdf_Resource_Image $image, $x1, $y1, $x2, $y2);
</pre>


        <p class="para">
            Image objects should be created with
             <span class="methodname">Zend_Pdf_Image::imageWithPath($filePath)</span> method (JPG, PNG and
            TIFF images are supported now):
        </p>

        <div class="example" id="zend.pdf.drawing.image-drawing.example-1"><div class="info"><p><b>Example #13 Image drawing</b></p></div>
            

            <pre class="programlisting brush: php">
...
// load image
$image = Zend_Pdf_Image::imageWithPath(&#039;my_image.jpg&#039;);

$pdfPage-&gt;drawImage($image, 100, 100, 400, 300);
...
</pre>

        </div>

        <p class="para">
            <em class="emphasis">Important! JPEG support requires <acronym class="acronym">PHP</acronym> GD extension to be
                configured.</em><em class="emphasis">Important! PNG support requires ZLIB extension to be
                configured to work with Alpha channel images.</em>
        </p>

        <p class="para">
            Refer to the <acronym class="acronym">PHP</acronym> documentation for detailed information (<a href="http://www.php.net/manual/en/ref.image.php" class="link external">&raquo; http://www.php.net/manual/en/ref.image.php</a>).
            (<a href="http://www.php.net/manual/en/ref.zlib.php" class="link external">&raquo; http://www.php.net/manual/en/ref.zlib.php</a>).
        </p>
    </div>

    <div class="section" id="zend.pdf.drawing.line-drawing-style"><div class="info"><h1 class="title">Line drawing style</h1></div>
        

        <p class="para">
            Line drawing style is defined by line width, line color and line dashing pattern.
            All of this parameters can be assigned by <span class="classname">Zend_Pdf_Page</span>
            class methods:
        </p>

        <pre class="programlisting brush: php">
/** Set line color. */
public function setLineColor(Zend_Pdf_Color $color);

/** Set line width. */
public function setLineWidth(float $width);

/**
 * Set line dashing pattern.
 *
 * Pattern is an array of floats:
 *     array(on_length, off_length, on_length, off_length, ...)
 * Phase is shift from the beginning of line.
 *
 * @param array $pattern
 * @param array $phase
 * @return Zend_Pdf_Page
 */
public function setLineDashingPattern($pattern, $phase = 0);
</pre>

    </div>

    <div class="section" id="zend.pdf.drawing.fill-style"><div class="info"><h1 class="title">Fill style</h1></div>
        

        <p class="para">
             <span class="methodname">Zend_Pdf_Page::drawRectangle()</span>,
             <span class="methodname">Zend_Pdf_Page::drawPolygon()</span>,
             <span class="methodname">Zend_Pdf_Page::drawCircle()</span> and
             <span class="methodname">Zend_Pdf_Page::drawEllipse()</span> methods take
            <var class="varname">$fillType</var> argument as an optional parameter. It can be:
        </p>

        <ul class="itemizedlist">
            <li class="listitem">
                <p class="para">Zend_Pdf_Page::SHAPE_DRAW_STROKE - stroke shape</p>
            </li>

            <li class="listitem">
                <p class="para">Zend_Pdf_Page::SHAPE_DRAW_FILL - only fill shape</p>
            </li>

            <li class="listitem">
                <p class="para">
                    Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE - fill and stroke (default behavior)
                </p>
            </li>
        </ul>

        <p class="para">
             <span class="methodname">Zend_Pdf_Page::drawPolygon()</span> methods also takes an additional
            parameter <var class="varname">$fillMethod</var>:
        </p>

        <ul class="itemizedlist">
            <li class="listitem">
                <p class="para">Zend_Pdf_Page::FILL_METHOD_NON_ZERO_WINDING (default behavior)</p>

                <p class="para">
                    <i class="citetitle">PDF reference</i> describes this rule as follows:

                    <blockquote class="blockquote">
                        <p class="para">
                            The nonzero winding number rule determines whether a given point is
                            inside a path by conceptually drawing a ray from that point to infinity
                            in any direction and then examining the places where a segment of the
                            path crosses the ray. Starting with a count of 0, the rule adds 1 each
                            time a path segment crosses the ray from left to right and subtracts 1
                            each time a segment crosses from right to left. After counting all the
                            crossings, if the result is 0 then the point is outside the path;
                            otherwise it is inside. Note: The method just described does not specify
                            what to do if a path segment coincides with or is tangent to the chosen
                            ray. Since the direction of the ray is arbitrary, the rule simply
                            chooses a ray that does not encounter such problem intersections. For
                            simple convex paths, the nonzero winding number rule defines the inside
                            and outside as one would intuitively expect. The more interesting cases
                            are those involving complex or self-intersecting paths like the ones
                            shown in Figure 4.10 (in a <acronym class="acronym">PDF</acronym> Reference). For a path
                            consisting of a five-pointed star, drawn with five connected straight
                            line segments intersecting each other, the rule considers the inside to
                            be the entire area enclosed by the star, including the pentagon in the
                            center. For a path composed of two concentric circles, the areas
                            enclosed by both circles are considered to be inside, provided that both
                            are drawn in the same direction. If the circles are drawn in opposite
                            directions, only the &quot;doughnut&quot; shape between them is inside, according
                            to the rule; the &quot;doughnut hole&quot; is outside.
                        </p>
                    </blockquote>
                </p>
            </li>

            <li class="listitem">
                <p class="para">Zend_Pdf_Page::FILL_METHOD_EVEN_ODD</p>

                <p class="para">
                    <i class="citetitle">PDF reference</i> describes this rule as follows:
                    <blockquote class="blockquote">
                        <p class="para">
                            An alternative to the nonzero winding number rule is the even-odd rule.
                            This rule determines the &quot;insideness&quot; of a point by drawing a ray from
                            that point in any direction and simply counting the number of path
                            segments that cross the ray, regardless of direction. If this number is
                            odd, the point is inside; if even, the point is outside. This yields the
                            same results as the nonzero winding number rule for paths with simple
                            shapes, but produces different results for more complex shapes. Figure
                            4.11 (in a <acronym class="acronym">PDF</acronym> Reference) shows the effects of
                            applying the even-odd rule to complex paths. For the five-pointed star,
                            the rule considers the triangular points to be inside the path, but not
                            the pentagon in the center. For the two concentric circles, only the
                            &quot;doughnut&quot; shape between the two circles is considered inside,
                            regardless of the directions in which the circles are drawn.
                        </p>
                    </blockquote>
                </p>
            </li>
        </ul>
    </div>

    <div class="section" id="zend.pdf.drawing.linear-transformations"><div class="info"><h1 class="title">Linear Transformations</h1></div>
        

        <div class="section" id="zend.pdf.drawing.linear-transformations.rotations"><div class="info"><h1 class="title">Rotations</h1></div>
            

            <p class="para">
                <acronym class="acronym">PDF</acronym> page can be rotated before applying any draw operation.
                It can be done by  <span class="methodname">Zend_Pdf_Page::rotate()</span> method:
            </p>

            <pre class="programlisting brush: php">
/**
 * Rotate the page.
 *
 * @param float $x  - the X co-ordinate of rotation point
 * @param float $y  - the Y co-ordinate of rotation point
 * @param float $angle - rotation angle
 * @return Zend_Pdf_Page
 */
public function rotate($x, $y, $angle);
</pre>

        </div>

        <div class="section" id="zend.pdf.drawing.linear-transformations.scale"><div class="info"><h1 class="title">Starting from ZF 1.8, scaling</h1></div>
            

            <p class="para">
                Scaling transformation is provided by
                 <span class="methodname">Zend_Pdf_Page::scale()</span> method:
            </p>

            <pre class="programlisting brush: php">
/**
 * Scale coordination system.
 *
 * @param float $xScale - X dimention scale factor
 * @param float $yScale - Y dimention scale factor
 * @return Zend_Pdf_Page
 */
public function scale($xScale, $yScale);
</pre>

        </div>

        <div class="section" id="zend.pdf.drawing.linear-transformations.translate"><div class="info"><h1 class="title">Starting from ZF 1.8, translating</h1></div>
            

            <p class="para">
                Coordinate system shifting is performed by
                 <span class="methodname">Zend_Pdf_Page::translate()</span> method:
            </p>

            <pre class="programlisting brush: php">
/**
 * Translate coordination system.
 *
 * @param float $xShift - X coordinate shift
 * @param float $yShift - Y coordinate shift
 * @return Zend_Pdf_Page
 */
public function translate($xShift, $yShift);
</pre>

        </div>

        <div class="section" id="zend.pdf.drawing.linear-transformations.skew"><div class="info"><h1 class="title">Starting from ZF 1.8, skewing</h1></div>
            

            <p class="para">
                Page skewing can be done using  <span class="methodname">Zend_Pdf_Page::skew()</span>
                method:
            </p>

            <pre class="programlisting brush: php">
/**
 * Translate coordination system.
 *
 * @param float $x  - the X co-ordinate of axis skew point
 * @param float $y  - the Y co-ordinate of axis skew point
 * @param float $xAngle - X axis skew angle
 * @param float $yAngle - Y axis skew angle
 * @return Zend_Pdf_Page
 */
public function skew($x, $y, $xAngle, $yAngle);
</pre>

        </div>
    </div>

    <div class="section" id="zend.pdf.drawing.save-restore"><div class="info"><h1 class="title">Save/restore graphics state</h1></div>
        

        <p class="para">
            At any time page graphics state (current font, font size, line color, fill color,
            line style, page rotation, clip area) can be saved and then restored. Save operation
            puts data to a graphics state stack, restore operation retrieves it from there.
        </p>

        <p class="para">
            There are two methods in <span class="classname">Zend_Pdf_Page</span> class for these
            operations:
        </p>

        <pre class="programlisting brush: php">
/**
 * Save the graphics state of this page.
 * This takes a snapshot of the currently applied style, position,
 * clipping area and any rotation/translation/scaling that has been
 * applied.
 *
 * @return Zend_Pdf_Page
 */
public function saveGS();

/**
 * Restore the graphics state that was saved with the last call to
 * saveGS().
 *
 * @return Zend_Pdf_Page
 */
public function restoreGS();
</pre>

    </div>

    <div class="section" id="zend.pdf.drawing.clipping"><div class="info"><h1 class="title">Clipping draw area</h1></div>
        

        <p class="para">
            <acronym class="acronym">PDF</acronym> and <span class="classname">Zend_Pdf</span> module support clipping of
            draw area. Current clip area limits the regions of the page affected by painting
            operators. It&#039;s a whole page initially.
        </p>

        <p class="para">
            <span class="classname">Zend_Pdf_Page</span> class provides a set of methods for clipping
            operations.
        </p>

        <pre class="programlisting brush: php">
/**
 * Intersect current clipping area with a rectangle.
 *
 * @param float $x1
 * @param float $y1
 * @param float $x2
 * @param float $y2
 * @return Zend_Pdf_Page
 */
public function clipRectangle($x1, $y1, $x2, $y2);
</pre>


        <pre class="programlisting brush: php">
/**
 * Intersect current clipping area with a polygon.
 *
 * @param array $x  - array of float (the X co-ordinates of the vertices)
 * @param array $y  - array of float (the Y co-ordinates of the vertices)
 * @param integer $fillMethod
 * @return Zend_Pdf_Page
 */
public function clipPolygon($x,
                            $y,
                            $fillMethod =
                                Zend_Pdf_Page::FILL_METHOD_NON_ZERO_WINDING);
</pre>


        <pre class="programlisting brush: php">
/**
 * Intersect current clipping area with a circle.
 *
 * @param float $x
 * @param float $y
 * @param float $radius
 * @param float $startAngle
 * @param float $endAngle
 * @return Zend_Pdf_Page
 */
public function clipCircle($x,
                           $y,
                           $radius,
                           $startAngle = null,
                           $endAngle = null);
</pre>


        <pre class="programlisting brush: php">
/**
 * Intersect current clipping area with an ellipse.
 *
 * Method signatures:
 * drawEllipse($x1, $y1, $x2, $y2);
 * drawEllipse($x1, $y1, $x2, $y2, $startAngle, $endAngle);
 *
 * @todo process special cases with $x2-$x1 == 0 or $y2-$y1 == 0
 *
 * @param float $x1
 * @param float $y1
 * @param float $x2
 * @param float $y2
 * @param float $startAngle
 * @param float $endAngle
 * @return Zend_Pdf_Page
 */
public function clipEllipse($x1,
                            $y1,
                            $x2,
                            $y2,
                            $startAngle = null,
                            $endAngle = null);
</pre>

    </div>

    <div class="section" id="zend.pdf.drawing.styles"><div class="info"><h1 class="title">Styles</h1></div>
        

        <p class="para">
            <span class="classname">Zend_Pdf_Style</span> class provides styles functionality.
        </p>

        <p class="para">
            Styles can be used to store a set of graphic state parameters and apply it to a
            <acronym class="acronym">PDF</acronym> page by one operation:
        </p>

        <pre class="programlisting brush: php">
/**
 * Set the style to use for future drawing operations on this page
 *
 * @param Zend_Pdf_Style $style
 * @return Zend_Pdf_Page
 */
public function setStyle(Zend_Pdf_Style $style);

/**
 * Return the style, applied to the page.
 *
 * @return Zend_Pdf_Style|null
 */
public function getStyle();
</pre>


        <p class="para">
            <span class="classname">Zend_Pdf_Style</span> class provides a set of methods to set or get
            different graphics state parameters:
        </p>

        <pre class="programlisting brush: php">
/**
 * Set line color.
 *
 * @param Zend_Pdf_Color $color
 * @return Zend_Pdf_Page
 */
public function setLineColor(Zend_Pdf_Color $color);
</pre>


        <pre class="programlisting brush: php">
/**
 * Get line color.
 *
 * @return Zend_Pdf_Color|null
 */
public function getLineColor();
</pre>


        <pre class="programlisting brush: php">
/**
 * Set line width.
 *
 * @param float $width
 * @return Zend_Pdf_Page
 */
public function setLineWidth($width);
</pre>


        <pre class="programlisting brush: php">
/**
 * Get line width.
 *
 * @return float
 */
public function getLineWidth();
</pre>


        <pre class="programlisting brush: php">
/**
 * Set line dashing pattern
 *
 * @param array $pattern
 * @param float $phase
 * @return Zend_Pdf_Page
 */
public function setLineDashingPattern($pattern, $phase = 0);
</pre>


        <pre class="programlisting brush: php">
/**
 * Get line dashing pattern
 *
 * @return array
 */
public function getLineDashingPattern();
</pre>


        <pre class="programlisting brush: php">
/**
 * Get line dashing phase
 *
 * @return float
 */
public function getLineDashingPhase();
</pre>


        <pre class="programlisting brush: php">
/**
 * Set fill color.
 *
 * @param Zend_Pdf_Color $color
 * @return Zend_Pdf_Page
 */
public function setFillColor(Zend_Pdf_Color $color);
</pre>


        <pre class="programlisting brush: php">
/**
 * Get fill color.
 *
 * @return Zend_Pdf_Color|null
 */
public function getFillColor();
</pre>


        <pre class="programlisting brush: php">
/**
 * Set current font.
 *
 * @param Zend_Pdf_Resource_Font $font
 * @param float $fontSize
 * @return Zend_Pdf_Page
 */
public function setFont(Zend_Pdf_Resource_Font $font, $fontSize);
</pre>


        <pre class="programlisting brush: php">
/**
 * Modify current font size
 *
 * @param float $fontSize
 * @return Zend_Pdf_Page
 */
public function setFontSize($fontSize);
</pre>


        <pre class="programlisting brush: php">
/**
 * Get current font.
 *
 * @return Zend_Pdf_Resource_Font $font
 */
public function getFont();
</pre>


        <pre class="programlisting brush: php">
/**
 * Get current font size
 *
 * @return float $fontSize
 */
public function getFontSize();
</pre>

    </div>

    <div class="section" id="zend.pdf.drawing.alpha"><div class="info"><h1 class="title">Transparency</h1></div>
        

        <p class="para">
            <span class="classname">Zend_Pdf</span> module supports transparency handling.
        </p>

        <p class="para">
            Transparency may be set using  <span class="methodname">Zend_Pdf_Page::setAlpha()</span> method:
        </p>

        <pre class="programlisting brush: php">
/**
 * Set the transparency
 *
 * $alpha == 0  - transparent
 * $alpha == 1  - opaque
 *
 * Transparency modes, supported by PDF:
 * Normal (default), Multiply, Screen, Overlay, Darken, Lighten,
 * ColorDodge, ColorBurn, HardLight, SoftLight, Difference, Exclusion
 *
 * @param float $alpha
 * @param string $mode
 * @throws Zend_Pdf_Exception
 * @return Zend_Pdf_Page
 */
public function setAlpha($alpha, $mode = &#039;Normal&#039;);
</pre>

    </div>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.pdf.pages.html">Working with Pages</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.pdf.html">Zend_Pdf</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.pdf.interactive-features.html">Interactive Features</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="reference.html">Zend Framework Reference</a></li>
  <li class="header up"><a href="zend.pdf.html">Zend_Pdf</a></li>
  <li><a href="zend.pdf.introduction.html">Introduction</a></li>
  <li><a href="zend.pdf.create.html">Creating and Loading PDF Documents</a></li>
  <li><a href="zend.pdf.save.html">Save Changes to PDF Documents</a></li>
  <li><a href="zend.pdf.pages.html">Working with Pages</a></li>
  <li class="active"><a href="zend.pdf.drawing.html">Drawing</a></li>
  <li><a href="zend.pdf.interactive-features.html">Interactive Features</a></li>
  <li><a href="zend.pdf.info.html">Document Info and Metadata</a></li>
  <li><a href="zend.pdf.usage.html">Zend_Pdf module usage example</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>